//
//  FeedbackViewController.m
//  IconMaster
//
//  Created by mac on 2017/7/28.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "FeedbackViewController.h"
#import <sys/utsname.h>


@interface FeedbackViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *whiteBackView;
@property (weak, nonatomic) IBOutlet UITextView *ideaTextView;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (weak, nonatomic) IBOutlet UIView *adView;

@property (nonatomic, strong) NSDictionary *feedBackDict;

@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.whiteBackView.layer.cornerRadius = 5.0f;
    self.whiteBackView.layer.masksToBounds = YES;
    self.ideaTextView.layer.cornerRadius = 5.0f;
    self.ideaTextView.layer.masksToBounds = YES;
    self.ideaTextView.delegate = self;
    self.ideaTextView.layer.borderColor = [UIColor grayColor].CGColor;
    self.ideaTextView.layer.borderWidth = 1.0f;
    self.confirmBtn.layer.cornerRadius = 5.0f;
    self.confirmBtn.layer.masksToBounds = YES;
    [self setBarType];
    
    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        //
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];  
        CFShow((__bridge CFTypeRef)(infoDictionary));  
        // app名称  
        NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];  
        // app版本  
        NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];  
        // app build版本  
        NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];  
        
        
        
        //手机序列号  
//        NSString* identifierNumber =  [[UIDevice currentDevice] uniqueIdentifier];  
//        NSLog(@"手机序列号: %@",identifierNumber);  
        //手机别名： 用户定义的名称  
        NSString* userPhoneName = [[UIDevice currentDevice] name];  
        NSLog(@"手机别名: %@", userPhoneName);  
        //设备名称  
        NSString* deviceName = [[UIDevice currentDevice] systemName];  
        NSLog(@"设备名称: %@",deviceName );  
        //手机系统版本  
        NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];  
        NSLog(@"手机系统版本: %@", phoneVersion);  
        //手机型号  
        NSString* phoneModel = [[UIDevice currentDevice] model];  
        NSLog(@"手机型号: %@",phoneModel );  
        //地方型号  （国际化区域名称）  
//        NSString* localPhoneModel = [[UIDevice currentDevice] localizedModel];  
//        NSLog(@"国际化区域名称: %@",localPhoneModel );  
//        
//        NSDictionary *infoDictionary1 = [[NSBundle mainBundle] infoDictionary];  
//        // 当前应用名称  
//        NSString *appCurName = [infoDictionary1 objectForKey:@"CFBundleDisplayName"];  
////        NSLog(@"当前应用名称：%@",appCurName);  
//        // 当前应用软件版本  比如：1.0.1  
//        NSString *appCurVersion = [infoDictionary1 objectForKey:@"CFBundleShortVersionString"];  
////        NSLog(@"当前应用软件版本:%@",appCurVersion);  
//        // 当前应用版本号码   int类型  
//        NSString *appCurVersionNum = [infoDictionary1 objectForKey:@"CFBundleVersion"];  
//        NSLog(@"当前应用版本号码：%@",appCurVersionNum); 
        
        self.feedBackDict = @{@"deviceBrand":@"苹果",
                              @"deviceVersion":[self deviceModelName] ? [self deviceModelName] :@"没有",
                              @"systemVersion":phoneVersion,
                              @"appVersion":app_Version};
    });
    NSLog(@"deviceModelName == %@", [self deviceModelName]);
    [self initAdmob];
    // Do any additional setup after loading the view.
}
- (IBAction)confirmBtnAction:(id)sender {
    
    [IconmasterHttp GreatNetWorkingRequestType:GreatRequestType_GET andurl:FeedbackAdd andparameters:@{@"feedbackContent":self.ideaTextView.text,@"deviceBrand":self.feedBackDict[@"deviceBrand"], @"deviceVersion":self.feedBackDict[@"deviceVersion"],@"systemVersion":self.feedBackDict[@"systemVersion"],@"appVersion":self.feedBackDict[@"appVersion"]} andcomplete:^(id json, NSError *error) {
            //
            if ([json[@"code"] integerValue] == 200) {
                //
                [self alterAction:@"提交成功,谢谢您的意见"];
            }
        }];
     
   
}
- (void)initAdmob
{
    GADBannerView* bannerView =
    [AdAndHint showBannerViewInController:self view:self.view frame:CGRectZero];
    CGRect bannerFrame = bannerView.frame;
    bannerFrame.origin.x = 0;
    bannerFrame.origin.y = KHeight -10-bannerFrame.size.height;
    bannerView.frame = bannerFrame;
    [self.adView addSubview:bannerView];
    bannerView.sd_layout.topEqualToView(self.adView).leftEqualToView(self.adView).rightEqualToView(self.adView).bottomEqualToView(self.adView);
    //    bannerView.autoresizingMask = segmentControl.autoresizingMask;
    
    [AdAndHint loadInterstitialIfNeed];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    textView.text = nil;
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
 
    [self.view endEditing:YES];
}
- (BOOL) isEmpty:(NSString *) str {
    
    if(!str) {
        
        return true;
        
    }else {
        
        
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and next line characters (U+000A–U+000D,U+0085).
        
        
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        
        
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        
        
        if([trimedString length] == 0) {
            
            return true;
            
            
        }else {
            
            
            return false;
            
            
        }
        
    }
    
    
}

- (NSString*)deviceModelName

{
    
    struct utsname systemInfo;
    
    uname(&systemInfo);
    
    NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    
    
    //iPhone 系列
    
    if ([deviceModel isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    
    if ([deviceModel isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    
    if ([deviceModel isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    
    if ([deviceModel isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    
    if ([deviceModel isEqualToString:@"iPhone3,2"])    return @"Verizon iPhone 4";
    
    if ([deviceModel isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    
    if ([deviceModel isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
    
    if ([deviceModel isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
    
    if ([deviceModel isEqualToString:@"iPhone5,3"])    return @"iPhone 5C";
    
    if ([deviceModel isEqualToString:@"iPhone5,4"])    return @"iPhone 5C";
    
    if ([deviceModel isEqualToString:@"iPhone6,1"])    return @"iPhone 5S";
    
    if ([deviceModel isEqualToString:@"iPhone6,2"])    return @"iPhone 5S";
    
    if ([deviceModel isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    
    if ([deviceModel isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    
    if ([deviceModel isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    
    if ([deviceModel isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    
    if ([deviceModel isEqualToString:@"iPhone9,1"])    return @"iPhone 7 (CDMA)";
    
    if ([deviceModel isEqualToString:@"iPhone9,3"])    return @"iPhone 7 (GSM)";
    
    if ([deviceModel isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus (CDMA)";
    
    if ([deviceModel isEqualToString:@"iPhone9,4"])    return @"iPhone 7 Plus (GSM)";
    
    return deviceModel;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
